import LogFactory from "../log/LogFactory.js";
import RequestHandler from "../util/RequestHadler.js";
import { IDENTITY_MAP } from "./constant.js";

const log = LogFactory.getLogger("DashboardResolver");
const _TOTAL = "Total";
const _WINS = "Wins";
const CARD_TITLE = {
    "ma": "妈妈计分板",
    "jun": "大军计分板",
    "chang": "畅畅计分板"
};
/**
 * 游戏仪表板解析器
 */
export default class DashboardResolver {
    // 游戏总局数
    dt = null; 
    // 游戏mvp
    dashboardMvp = null;
    // mvp计分板标题
    dashboardMvpName = null;
    // mvp计分板总金额
    dashboardMvpMoney = null;
    // mvp计分板地主场次
    dashboardMvpLondlord = null;
    // mvp计分板胜利场次
    dashboardMvpWinner = null;
    // 第二名计分板
    dashboardThirdTitle = null;
    dashboardThirdMoney = null;
    dashboardThirdLandlord = null;
    dashboardThirdWinner = null;
     // 第三名计分板
    dashboardSecondTitle = null;
    dashboardSecondMoney = null;
    dashboardSecondLandlord = null;
    dashboardSecondWinner = null;

    // mvp
    mvpPlayer = "";
    // 第二
    secondPlayer = "";
    //第三名
    thirdPlayer = "";
    // 解析后的仪表板使用的数据
    #data = null;
    // 名次数组
    #ranking = null;
    
    init() {
        // log.debug("游戏仪表板初始化");
        this.dt = $("#dashboardTotal");
        this.dashboardMvp = $("#dashboardMvp");
        this.dashboardMvpName = $("#dashboardMvpName");
        this.dashboardMvpMoney = $("#dashboardMvpMoney");
        this.dashboardMvpLondlord = $("#dashboardMvpLondlord");
        this.dashboardMvpWinner = $("#dashboardMvpWinner");
        this.dashboardThirdTitle = $("#dashboardThirdTitle");
        this.dashboardThirdMoney = $("#dashboardThirdMoney");
        this.dashboardThirdLandlord = $("#dashboardThirdLandlord");
        this.dashboardThirdWinner = $("#dashboardThirdWinner");
        this.dashboardSecondTitle = $("#dashboardSecondTitle");
        this.dashboardSecondMoney = $("#dashboardSecondMoney");
        this.dashboardSecondLandlord = $("#dashboardSecondLandlord");
        this.dashboardSecondWinner = $("#dashboardSecondWinner");
    }
    /**
     * 渲染仪表板数据
     * @param {*} total 游戏总局数 
     * @param {*} userLandlord  用户地主场次信息
     * @param {*} userTotal 用户计分合计信息
     */
    renderData(total,userLandlord,userTotal){
        this.analysisData(userLandlord,userTotal);
        this.dt.text(total);
        this.parseMvp(userTotal);
        this.parseDashboard();
    }
    /**
     * 解析数据
     * @param {*} userLandlord 
     * @param {*} userTotal 
     */
    analysisData(userLandlord,userTotal){
        if(userLandlord.length !== 3){
            let changData = userLandlord.find(e => e.landlordName === "CHANG");
            let junData = userLandlord.find(e => e.landlordName === "JUN");
            let maData = userLandlord.find(e => e.landlordName === "Ma");
            if(junData === void 0){
                userLandlord.push({
                    landlordName: "JUN",
                    landlordCount: 0
                });
            }
            if(maData === void 0){
                userLandlord.push({
                    landlordName: "MA",
                    landlordCount: 0
                });
            }
            if(changData === void 0){
                userLandlord.push({
                    landlordName: "CHANG",
                    landlordCount: 0
                });
            }
        }
        this.#data = userLandlord.map(element => {
            let kp_ = element.landlordName.toLowerCase();
             let moneyKey = kp_ + _TOTAL;
             let victoryKey = kp_ + _WINS;
             return {
                 name: element.landlordName, 
                 // 地主次数
                 landlordCount: element.landlordCount,
                 // 总金额
                 moneyTotal: userTotal[moneyKey],
                 // 胜利次数
                 victoryCount: userTotal[victoryKey],
                 //计分板名字
                 cardTitle: CARD_TITLE[kp_]
             };
         });
    
         let changTotal = parseInt(userTotal.changTotal);
         let junTotal = parseInt(userTotal.junTotal);
         let maTotal = parseInt(userTotal.maTotal);
         this.#ranking =  [
            {name: "chang", value: changTotal},
            {name: "jun", value: junTotal},
            {name: "ma", value: maTotal},
        ].sort((a, b) => b.value - a.value);
        // console.log(this.#ranking)
    }

    /**
     * 解析仪表板总卡片需要的数据
     * @param {*} userLandlord 
     * @param {*} userTotal 
     */
    parseDashboard(){
        // log.debug(this.#data,this.thirdPlayer,this.secondPlayer,this.mvpPlayer);
        this.parseMvpCard();
        this.parseSecondCard();
        this.parseThirdCard();
    }
    parseThirdCard(){
        let data = this.#data.find(e => e.name === this.thirdPlayer);
        if(!data){
            return;
        }
        this.dashboardThirdTitle.text(data.cardTitle);
        this.dashboardThirdMoney.text(data.moneyTotal/100);
        this.dashboardThirdLandlord.text(data.landlordCount);
        this.dashboardThirdWinner.text(data.victoryCount);
    }
    parseSecondCard(){
        let data = this.#data.find(e => e.name === this.secondPlayer);
        if(!data){
            return;
        }
        this.dashboardSecondTitle.text(data.cardTitle);
        this.dashboardSecondMoney.text(data.moneyTotal/100);
        this.dashboardSecondLandlord.text(data.landlordCount);
        this.dashboardSecondWinner.text(data.victoryCount);
    }
    parseMvpCard(){
        // log.debug("解析MVP仪表板", this.#data);
        let mvpData = this.#data.find(e => e.name === this.mvpPlayer);
        if(!mvpData){
            return;
        }
        this.dashboardMvpName.text(mvpData.cardTitle);
        this.dashboardMvpMoney.text(mvpData.moneyTotal/100);
        this.dashboardMvpLondlord.text(mvpData.landlordCount);
        this.dashboardMvpWinner.text(mvpData.victoryCount); 
    }

    /**
     * 解析mvp
     * @param {*} userTotal 用户计分合计信息
     */
    parseMvp(){
        this.mvpPlayer = this.#ranking[0]['name'].toUpperCase();
        this.secondPlayer = this.#ranking[1]['name'].toUpperCase();
        this.thirdPlayer = this.#ranking[2]['name'].toUpperCase();
        this.dashboardMvp.text(IDENTITY_MAP.get(this.mvpPlayer))
    }
}